package com.sdkj.recruit.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import com.sdkj.recruit.dao.IMongodbDAO;
import com.sdkj.recruit.dao.IReleaseVersionDao;
import com.sdkj.recruit.model.Paginate;
import com.sdkj.recruit.model.ReleaseVersion;
import com.sdkj.recruit.mongoSupport.QueryParamCreator;
import com.sdkj.recruit.util.StringUtil;

@Persistent
@Service("versionDao")
public class ReleaseVersionDao implements IReleaseVersionDao{
	@Autowired
	private IMongodbDAO dao;

	@Override
	public ReleaseVersion queryVersion(String versionNo) {
		return dao.getPO(ReleaseVersion.class, QueryParamCreator.eq("appVersion", versionNo));
	}

	@Override
	public void releaseVersion(ReleaseVersion version) {
		dao.saveOrUpdatePO(version);
	}

	@Override
	public void deleteVersion(String versionId) {
		dao.deletePOList(ReleaseVersion.class, QueryParamCreator.eq("versionId", versionId));
	}

	@Override
	public List<ReleaseVersion> queryVersionList(int pageNo, int pageSize, String keyWord) {
		List<Order> orders = new ArrayList<Order>();
		Order order = new Order(Direction.DESC, "releaseDate");
		orders.add(order);
		
		List<Criteria> condList = new ArrayList<Criteria>();
		
		if(!StringUtil.isEmptyString(keyWord)){
			condList.add(QueryParamCreator.like("appVersion", keyWord));
		}
		
		condList.add(QueryParamCreator.isNotEmpty("appVersion"));
		return dao.getPOList(ReleaseVersion.class, new Paginate(pageNo, pageSize), orders, (Criteria[]) condList.toArray(new Criteria[0]));
	}

	@Override
	public long queryAllVersionCount(String keyWord) {
		List<Criteria> condList = new ArrayList<Criteria>();
		if(!StringUtil.isEmptyString(keyWord)){
			condList.add(QueryParamCreator.like("appVersion", keyWord));
		}
		condList.add(QueryParamCreator.ne("appVersion", null));
		return dao.getRowCount(ReleaseVersion.class, (Criteria[]) condList.toArray(new Criteria[0]));
	}
	
}
